// Please do not use this code without permission from the developer

$(function() {

    var switchMode  = $('#render'),
        txtPreview  = $('#textPreview'),
        editor      = $('#htmlsource').hide(),
        generate    = $('#generate'),
        balas       = $('#balas'),
        anchorBalas = $('a.answer-this-comment'),
        inputBalas  = $('#userID'),
        clear       = $('#comment-delete');


    switchMode.click(function() {
        switchEditor();
        if($(this).text() == "Konversi Tampilan") {
            $(this).text("Kembali");
        } else {
            $(this).text("Konversi Tampilan");
        }
        $('.toggle').toggle();
        return false;
    });


    clear.click(function() {
        var yakin = confirm("Anda yakin?");
        if(yakin) {
            txtPreview.html("");
            editor.val("");
        }
        return false;
    });
    balas.submit(function() {
        var val = $('input', this).val() + " ";
        txtPreview.prepend(val);
        $('input', this).val("");
        $('html,body').animate({scrollTop:$('#main').offset().top}, 400);
        switchMode.fadeIn();
        return false;
    });
    inputBalas.focus(function() {
        switchMode.fadeOut();
    }).blur(function() {
        switchMode.fadeIn();
    });
    anchorBalas.click(function() {
        var p = prompt("Masukkan userID komentator yang ingin Anda balas:");
        if(p) {
            editor.val(p + " " + editor.val());
            txtPreview.prepend(p + " ");
            $('html,body').animate({scrollTop:$('#main').offset().top}, 400);
        }
        return false;
    });

    function insertCode(button,newElem) {
        $(button).click(function() {
            txtPreview.append(newElem);
            return false;
        });
    }
    insertCode('a.code', '<code>&lt;sampel&gt;</code>&nbsp;');
    insertCode('a.pre', '<pre>Kode Anda di sini... (tidak perlu dikonversi)</pre><br>');
    insertCode('a.blockquote', '<br><blockquote>Teks Anda di sini...</blockquote><br>');
    insertCode('a.note', '<br><cite class="note">Teks Anda di sini...</cite>');
    $('a.link').click(function() {
        var form = prompt("Sisipkan URL Halaman:");
        if(form) txtPreview.append('<a class="anchor" href="'+form+'">[Link]</a>&nbsp;');
        return false;
    });
    $('a.image').click(function() {
        var form = prompt("Sisipkan URL Gambar:");
        if(form) txtPreview.append('<br><img class="incomment" src="'+form+'" /><br>');
        return false;
    });
    
    function switchEditor() {
        if(txtPreview.is(':visible')) {
            var htm = txtPreview.html()
                .replace(/<br>/g, "\n")
                .replace(/>&nbsp;/g, "> ")
                .replace(/<a class=\"anchor\" href=\"(.[^>]*)\">(.[^>]*)<\/a>/g, "<i rel=\"anchor\">$1<\/i>")
                .replace(/<img class=\"incomment\" src=\"(.[^>]*)\">/g, "<i rel=\"image\">$1<\/i>")
                .replace(/<code>(.[^>]*)<\/code>/g, "<i rel=\"code\">$1<\/i>")
                .replace(/<pre>(.[^>]*)<\/pre>/g, "<i rel=\"pre\">$1<\/i>")
                .replace(/<blockquote>(.[^>]*)<\/blockquote>/g, "<b rel=\"quote\">$1<\/b>")
                .replace(/<cite class/g, "<em rel")
                .replace(/<\/cite>/g, "\<\/em>")
                .replace(/\t/g, "    ")
                // Chrome & Opera
                .replace(/<(div|p)>/g, "")
                .replace(/<\/(div|p)>/g, "\n");
            editor.val(htm);
        } else {
            var txt = editor.val()
                .replace(/\n/g, "<br>")
                .replace(/<i rel=\"anchor\">(.[^>]*)<\/i>/g, "<a class=\"anchor\" href=\"$1\">\[Link\]<\/a>")
                .replace(/<i rel=\"image\">(.[^>]*)<\/i><br>/g, "<img class=\"incomment\" src=\"$1\" \/><br>")
                .replace(/<i rel=\"code\">(.[^>]*)<\/i>/g, "<code>$1<\/code>")
                .replace(/<i rel=\"pre\">(.[^>]*)<\/i>/gm, "<pre>$1<\/pre>")
                .replace(/<b rel=\"quote\">(.[^>]*)<\/b>/g, "<blockquote>$1<\/blockquote>")
                .replace(/<em rel=\"note\">(.[^>]*)<\/em>/g, "<cite class=\"note\">$1<\/cite>")
                .replace(/\t/g, "    ");
            txtPreview.html(txt);
        }
    }

    txtPreview.one("click", function() {
        $(this).html("").removeAttr('title');
    });
	$(window).load(function() {
		$('#editor-mode li .anc .tooltip').delay(1000)
		.animate({opacity:"show",marginTop:10}, 200)
		.delay(7000)
		.animate({opacity:"hide",marginTop:40}, 200)
		.click(function() {
			$(this).remove();
		});
	});


});